package acs.bridge.jreport;

import acs.bridge.BridgeException;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FileTransferClient;

import javax.mail.MessagingException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

/**
 * acs.bridge.jreport
 * User: KuznetsovStV
 * Date: 04.04.14
 * Time: 10:13
 */
public class JReportSaveFtp extends JReportFileUse implements JReportSaveInterface {

    private String username;
    private String host;

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    private String filePath;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private String port;
    private String password;

    @Override
    public void saveReport(byte[] data, String extension) throws IOException, MessagingException {
    }

    @Override
    public void setReportName(String reportName) {
        setFileName(reportName);
    }

    @Override
    public String getReportName() {
        return fileName;
    }

    @Override
    public void saveReportItem(Collection<JReportItem> jReportItemCollection) throws Exception {

        FileTransferClient ftp = null;

        byte[] outdata = archiveReport(jReportItemCollection);
        String fullFileName = getFullFileName(utilityArcInterface.getExtension());

        try {
            ftp = new FileTransferClient();

            ftp.setRemoteHost(host);

            ftp.setUserName(username);
            ftp.setPassword(password);
            ftp.setTimeout(60000); // timeout  1 min
            ftp.connect();
            logger.info("FTP session connected");

            ftp.changeDirectory(filePath);
            OutputStream out = ftp.uploadStream(fullFileName);
            try {
                out.write(outdata);
            } finally {
                out.close();
            }
            logger.info(fullFileName + " uploaded");
        } catch (FTPException e) {
            logger.error(e.getMessage() + " (FTPException)");
            logger.error(e.getStackTrace());
            throw new BridgeException(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage() + " (IOException)");
            logger.error(e.getStackTrace());
            throw new BridgeException(e.getMessage());
        } finally {
            if (ftp != null) {
                ftp.disconnect();
                logger.info("FTP session disconnected");
            }
        }


    }
}
